package cn.supermaster.management.web.service.monitor.impl;

import cn.supermaster.management.common.enums.OnlineStatus;
import cn.supermaster.management.common.page.TableDataInfo;
import cn.supermaster.management.common.response.WebResponse;
import cn.supermaster.management.common.text.Convert;
import cn.supermaster.management.common.utils.ShiroUtils;
import cn.supermaster.management.common.utils.WebHelper;
import cn.supermaster.management.core.session.OnlineSession;
import cn.supermaster.management.core.session.OnlineSessionDAO;
import cn.supermaster.management.system.domain.SystemUserOnline;
import cn.supermaster.management.system.service.ISystemUserOnlineService;
import cn.supermaster.management.web.service.monitor.IMonitorUserOnlineService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * All rights Reserved, Designed By www.super-bytecode.com
 *
 * @ProjectName: supermaster-management
 * @Package: cn.supermaster.management.web.service.monitor.impl
 * @ClassName: MonitorUserOnlineServiceImpl
 * @Description: []
 * <p> </p>
 * @Author: [Lance Ting]
 * @Date: 2021/1/10 16:53
 * @Version: V1.0
 * @Copyright: 2021 www.super-bytecode.comn Inc. All rights reserved.
 * TODO: 注意,本文件Lance Ting所作,如果转载或使用请标明具体出处!
 **/
@Service
public class MonitorUserOnlineServiceImpl implements IMonitorUserOnlineService {

    private String prefix = "monitor/online";

    @Autowired
    private ISystemUserOnlineService userOnlineService;

    @Autowired
    private OnlineSessionDAO onlineSessionDAO;

    /*
     * ================================================
     *  页面跳转功能实现
     * ================================================
     */

    /**
     * @Description: [跳转到在线用户监控界面]
     * <p></P>
     * @Title: skipUserOnlineHtml
     * @Author: Lance Ting
     * @Date: 2021-01-10
     * @Return: java.lang.String
     */
    @Override
    public String skipUserOnlineHtml() {
        return prefix + "/online";
    }

    /*
     * ================================================
     *  Rest功能实现
     * ================================================
     */

    /**
     * @Description: [获取在线用户列表]
     * <p></P>
     * @Title: queryUserOnlineInformations
     * @Author: Lance Ting
     * @Date: 2021-01-10
     * @Param: user
     * @Return: cn.supermaster.management.common.page.TableDataInfo
     */
    @Override
    public TableDataInfo queryUserOnlineInformations(SystemUserOnline user) {
        WebHelper.startPage();
        List<SystemUserOnline> list = userOnlineService.selectUserOnlineList(user);

        TableDataInfo dataTable = WebHelper.getDataTable(list);
        //
        return dataTable;
    }

    /**
     * @Description: [强制剔除在线账户]
     * <p></P>
     * @Title: doBatchForceAccountLogout
     * @Author: Lance Ting
     * @Date: 2021-01-10
     * @Param: ids
     * @Return: cn.supermaster.management.common.response.WebResult
     */
    @Override
    public WebResponse doBatchForceAccountLogout(String ids) {
        String[] sessionIds = Convert.toStrArray(ids);
        for (String sessionId : sessionIds) {
            SystemUserOnline online = userOnlineService.selectOnlineById(sessionId);
            if (online == null) {
                return WebHelper.error("用户已下线");
            }
            OnlineSession onlineSession = (OnlineSession) onlineSessionDAO.readSession(online.getSessionId());
            if (onlineSession == null) {
                return WebHelper.error("用户已下线");
            }
            if (sessionId.equals(ShiroUtils.getSessionId())) {
                return WebHelper.error("当前登录用户无法强退");
            }
            onlineSessionDAO.delete(onlineSession);
            online.setStatus(OnlineStatus.off_line);
            userOnlineService.saveOnline(online);
            userOnlineService.removeUserCache(online.getuserName(), sessionId);
        }
        return WebHelper.success();
    }
}